///@file FileUtils.cpp
///@brief Implementation of methods for file management
///@author Arnaud Duval
///@version 0.0
///@date 2010/03/23

#include "../include/FileUtils.h"


///@brief Test existence of a file
///@brief fname Name of the file to test
bool FileExists(std::string fname)
{
	std::ifstream file(fname.c_str());
	if(file)
	{
		file.close();
		return true;
	}
	return false;
}

///@brief Replace a string in a file by another one
///@param file path to file
///@param s1 string to replace
///@param s2 replacing string
///@warning This routine use the sed command. It should be better to code a portable solution
///@warning Must be tested
void ReplaceStringInFile(std::string file, std::string s1, std::string s2)
{
	std::system(("sed -i \"s/"+s1+"/"+s2+"/\" "+file).c_str());
}

///@brief Count the number of lines in a file
///@param filename Name of the concerned file
unsigned int CountLines(std::string filename)
{
	unsigned int count = 0;
	std::string s;
	std::ifstream file(filename.c_str());

	while (std::getline(file, s))
		count++;

	return count;
}

